//
//  DataBaseManager.m
//  UI22_数据库
//
//  Created by 151129-49宋强 on 16/1/7.
//  Copyright © 2016年 www.lanou3g.com蓝鸥. All rights reserved.
//

#import "DataBaseManager.h"

@implementation DataBaseManager
+ (DataBaseManager *)shareInstance
{
    //静态指针变量 保证指针只有一个
    static DataBaseManager *dbManager = nil;
    //是否是第一次调用该方法
    //如果指针中为空 创建对象
    if (!dbManager) {
        //单例中的alloc不需要release
        dbManager = [[DataBaseManager alloc] init];
    }
    //如果指针不为空 直接返回
    return dbManager;
}
///打开数据库
- (void)openDB
{
    //数据库一般放在Document下
    //后缀 db/sqlite
    NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject stringByAppendingPathComponent:@"suibian.db"];
    //创建数据库文件
    //判断dbPoint指针中 是否存在数据库 如果不存在 在创建
    //参数1: 数据库路径(UTF8转码)
    // 参数2:数据库指针地址
     int result = sqlite3_open(path.UTF8String, &dbPoint);
    NSLog(@"打开数据库:%d, 路径: %@", result, path);
    
}
///关闭数据库
- (void)closeDB
{
    sqlite3_close(dbPoint);
}
///创建表
- (void)createTable
{
    //1.创建SQL语句
    //primary key 主键
    //autoincrement 自动增加
    NSString *createSQL = @"CREATE TABLE IF NOT EXISTS movie (movieName text, pubdate text, movieId integer PRIMARY KEY AUTOINCREMENT)";
    //执行语句
    //参数1: 数据库指针
    //参数2: sql语句
    int result = sqlite3_exec(dbPoint, createSQL.UTF8String, NULL, NULL, NULL);
    //3.判断
    if (result == SQLITE_OK) {
        NSLog(@"创建表成功");
    }else{
        NSLog(@"失败: %d", result);
    }
    

}
///删除表
- (void)dropTable
{
    //1.SQL
    NSString *dropSQl = @"DROP TABLE movie";
    //2.执行
    int result = sqlite3_exec(dbPoint, dropSQl.UTF8String, NULL, NULL, NULL);
    //3.判断
    [self judgeResult:result messager:@"删除表"];
    
}
///增
- (void)insertMovie:(Movie *)m
{
    //1.SQL
    NSString *insertSQL = [NSString stringWithFormat:@"INSERT INTO movie (movieName, pubdate) values('%@', '%@')", m.movieName, m.pubdate];
    //2.执行
    int result = sqlite3_exec(dbPoint, insertSQL.UTF8String, NULL, NULL, NULL);
    //3.判断
    [self judgeResult:result messager:@"插入数据"];
    
}
///删
- (void)deleteMovieWithId:(NSInteger)movieId
{
    //1.SQL
    NSString *deleteSQL = [NSString stringWithFormat:@"DELETE FROM movie WHERE movieId = '%ld'",movieId];
    //2.执行
    int result = sqlite3_exec(dbPoint, deleteSQL.UTF8String, NULL, NULL, NULL);
    
    //3.判断
    [self judgeResult:result messager:@"删除数据"];
}
///更新
- (void)updateMovieWith:(Movie *)newM withId:(NSInteger)movieId
{
    NSString *updateSQL = [NSString stringWithFormat:@"UPDATE movie SET movieName = '%@', pubdate = '%@' WHERE movieId = '%ld'", newM.movieName, newM.pubdate, movieId];
    int result = sqlite3_exec(dbPoint, updateSQL.UTF8String, NULL, NULL, NULL);
    [self judgeResult:result messager:@"更新数据"];
    
}
///查询
- (NSMutableArray *)select
{
    //查询后 数据处理过程
    //1.获取到所有的数据
    //2.遍历行 得到每一条数据
    //3.在遍历当前行的 每一列 按照对应关系保存在model中
    //4. model 添加到数组 返回
    
    //1.SQL语句
    NSString *selectSQL = @"SELECT * FROM movie";
    //2.创建数据库替身
    sqlite3_stmt *stmt = nil;
    //3.准备SQL语句
    //prepare_v2函数 把数据库对象/SQL语句/数据库替身/ 关联在一起
    int result = sqlite3_prepare_v2(dbPoint, selectSQL.UTF8String, -1, &stmt, nil);
    //4.初始化返回的数组
    NSMutableArray *arr = [NSMutableArray array];
    //5.判断准备是否成功
    if (result == SQLITE_OK) {
        NSLog(@"查询准备成功");
        //6.遍历每一行数据
        while (sqlite3_step(stmt) == SQLITE_ROW) {
            // 逐行获取每一列信息
            //列数 从0开始
            const unsigned char *mName = sqlite3_column_text(stmt, 0);
            const unsigned char *pubdate = sqlite3_column_text(stmt, 1);
            int mId = sqlite3_column_int(stmt, 3);
            //创建model
            Movie *m = [[Movie alloc] init];
            m.movieName = [NSString stringWithUTF8String:(const char *)mName];
            m.pubdate = [NSString stringWithUTF8String:(const char *)pubdate];
            m.movieId = mId;
            //添加到数组
            [arr addObject:m];
               }
        
    }else {
        NSLog(@"失败:%d", result);
    }
    return arr;
}





/// 判断方法
//参数1:判断的数值
//参数2:打印的信息
- (void)judgeResult:(int)r messager: (NSString *)mes
{
    if (r == SQLITE_OK) {
        NSLog(@"%@成功", mes);
    }else{
        NSLog(@"%@失败: %d",mes, r);
    }
}





@end

















